<?php

/**
 * Get link to the diff page between two revisions of a page
 *
 * @param string $page_name
 * @param int $revision
 *
 * @return string
 */
function link_to_diff($text, $page_name, $rev1, $rev2, $mode = 'inline')
{
  $url = nahoWikiPagePeer::url($page_name, null, 'diff', array(
    'revision_old' => $rev1,
    'revision_new' => $rev2,
    'mode'         => $mode,
    'raw'          => 0,
  ));

  return link_to($text, $url);
}

/**
 * Get link for the discussion page of a page in the wiki
 *
 * @param string $text text of the link
 * @param string $page_name
 * @param int $revision
 *
 * @return string
 */
function link_to_raw_diff($text, $page_name, $rev1, $rev2, $mode = 'unified')
{
  $url = nahoWikiPagePeer::url($page_name, null, 'diff', array(
    'revision_old' => $rev1,
    'revision_new' => $rev2,
    'mode'         => $mode,
    'raw'          => 1,
  ));

  return link_to($text, $url);
}

/**
 * Get URL for a page in the wiki
 *
 * @param string $page_name
 * @param int $revision
 * @param boolean $absolute
 *
 * @return string
 */
function url_for_wiki($page_name, $revision = null, $absolute = false, array $params = array())
{
  if (!is_null($revision))
  {
    $params['revision'] = $revision;
  }

  $url = nahoWikiPagePeer::url($page_name, null, 'view', $params);

  return url_for($url, $absolute);
}

/**
 * Get link for a page in the wiki
 *
 * @param string $text text of the link (if null, we create it from the pagename+revision)
 * @param string $page_name
 * @param array $options params added to the link (you can add a 'revision' option to specifiy the revision of the page you want to link to)
 *
 * @return string
 */
function link_to_wiki($text, $page_name, $options = array())
{
  if (is_string($options))
  {
    $options = sfToolkit::stringToArray($options);
  }

  $params = array();
  if (isset($options['revision']))
  {
    $params['revision'] = $options['revision'];
  }
  $url = nahoWikiPagePeer::url($page_name, null, 'view', $params);

  if (is_null($text))
  {
    $text = htmlspecialchars(nahoWikiPagePeer::getBasename($page_name));
    if (isset($options['revision']))
    {
      $text .= ' rev. ' . $options['revision'];
    }
  }

  return link_to($text, $url, $options);
}

/**
 * Get URL for the presentation page of a user in the wiki
 *
 * @param string $username
 * @param int $revision
 * @param boolean $absolute
 *
 * @return string
 */
function url_for_wiki_user($username, $revision = null, $absolute = false)
{
  return url_for_wiki('user:' . $username, $revision, $absolute);
}

/**
 * Get link for the presentation page of a user in the wiki
 *
 * @param string $text text of the link
 * @param string $username
 * @param array $options params added to the link (you can add a 'revision' option to specifiy the revision of the page you want to link to)
 * @param int $revision
 *
 * @return string
 */
function link_to_wiki_user($text, $username, $options = array())
{
  return link_to_wiki($text, 'user:' . $username, $options);
}

/**
 * Get URL for the discussion page of a page in the wiki
 *
 * @param string $page_name
 * @param int $revision
 * @param boolean $absolute
 *
 * @return string
 */
function url_for_wiki_discuss($page_name, $revision = null, $absolute = false)
{
  return url_for_wiki('discuss:' . $page_name, $revision, $absolute);
}

/**
 * Get link for the discussion page of a page in the wiki
 *
 * @param string $text text of the link
 * @param string $page_name
 * @param array $options params added to the link
 * @param int $revision
 *
 * @return string
 */
function link_to_wiki_discuss($text, $page_name, $options = array())
{
  return link_to_wiki($text, 'discuss:' . $page_name, $options);
}

/**
 * Include (display) a wiki page
 *
 * Options that can be used :
 * - display_toc => false # Hide the Table of contents
 * - display_pagename => false # Hide the page name
 *
 * @param string $page_name fully qualified page name
 * @param array $options additional options
 * @param int $revision
 */
function include_wiki($page_name, $revision = null, $options = array())
{
  $options['pagename'] = $page_name;
  $options['revision'] = $revision;

  include_component('nahoWiki', 'content', $options);
}

/**
 * Returns XHTML content of a wiki page
 *
 * Options that can be used :
 * - display_toc => false # Hide the Table of contents
 * - display_pagename => false # Hide the page name
 *
 * @param string $page_name fully qualified page name
 * @param array $options additional options
 * @param int $revision
 */
function get_wiki($page_name, $revision = null, $options = array())
{
  $options['pagename'] = $page_name;
  $options['revision'] = $revision;

  return get_component('nahoWiki', 'content', $options);
}

/**
 * Translates namespaces in pagename entries to longer, human-readable forms
 * which can be supplied via the app.yml. This is useful, if you use
 * abbreviations for namespaces to save typing work, but want to see full
 * namespace names in the breadcrumb bar.
 *
 * @author Jense Schulze
 * @param string $page_name The pagename used in links, including the namespaces seperated by :.
 * @param string $new_separator The new separator that will be used to mark namespaces in translated string
 * @return string The translated pagename
 */
function translate_namespaces($page_name, $new_separator = ':') {
  use_helper('I18N');

  $namespaces = sfConfig::get('app_nahoWikiPlugin_namespaces', array());

  $parts = explode(':', $page_name);
  $page_name = array_pop($parts); // exclude page name
  foreach ($parts as $ns)
  {
    if (isset($namespaces[$ns]))
    {
      $ns = __($namespaces[$ns]);
    }
    $page_name = $ns . $new_separator . $page_name;
  }

  return $page_name;
}
